#coding:utf-8


from twisted.internet import wxreactor
wxreactor.install()
from twisted.internet import reactor
import wx

import stock
from core import logging
from server import player, vtcp, vudp, vuser
from gui import serverform

INI_FILENAME = "config.ini"

def start_server():
    
    
    # 日志服务
    logging.setLogDirectory('log')
    
    p = player.Player()
    config = stock.ConfigIni(INI_FILENAME)
    option = 'port'
    
    user_default_port = 14556
    port = int(config.get("USER", option, user_default_port))
    reactor.listenTCP(port, vuser.UserServer(p))
    logging.info("user interface server started at port %s"%port)
    
    tcp_default_port = 9020
    port = int(config.get("TCP", option, tcp_default_port))
    reactor.listenTCP(port, vtcp.TCPServer(p))
    logging.info("vehicle tcp server started at port %s"%port)
    
    udp_default_port = 3100
    port = int(config.get("UDP", option, udp_default_port))
    reactor.listenUDP(port, vudp.UDPServer(p))
    logging.info("vehicle udp server started at port %s"%port)
    
    config.save()
    reactor.run()
    
class Vhserver(wx.App):
    def OnInit(self):
        self.tray = serverform.ServerTray(reactor)
        return True

if __name__ == '__main__':
    app = Vhserver(0)
    reactor.registerWxApp(app)
    start_server()
